<template>
  <el-dialog
    v-model="dialogVisible"
    :title="`属性管理 - ${data?.categoryName || ''}`"
    width="1000px"
    append-to-body
    @close="handleClose"
  >
    <!-- 操作按钮 -->
    <div class="mb8">
      <el-button type="primary" icon="Plus" @click="addProperty">新增属性</el-button>
      <el-button type="info" icon="Upload" @click="importProperty">导入属性</el-button>
    </div>

    <!-- 属性列表 -->
    <el-table :data="propertyList" border style="width: 100%">
      <el-table-column prop="identifier" label="标识符" width="120" />
      <el-table-column prop="name" label="属性名称" width="120" />
      <el-table-column prop="dataType" label="数据类型" width="100">
        <template #default="{ row }">
          <el-tag :type="getDataTypeTag(row.dataType)">
            {{ getDataTypeText(row.dataType) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="dataSpecs" label="数据定义" width="200" show-overflow-tooltip />
      <el-table-column prop="accessMode" label="读写类型" width="100">
        <template #default="{ row }">
          <el-tag :type="getAccessModeTag(row.accessMode)">
            {{ getAccessModeText(row.accessMode) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="required" label="是否必需" width="100">
        <template #default="{ row }">
          <el-tag :type="row.required ? 'success' : 'info'">
            {{ row.required ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" show-overflow-tooltip />
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" size="small" @click="editProperty(row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="deleteProperty(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 组件属性
interface Props {
  visible: boolean
  data?: any
  propertyList?: any[]
}

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  data: null,
  propertyList: () => []
})

// 组件事件
const emit = defineEmits<{
  'update:visible': [value: boolean]
  'add-property': []
  'import-property': []
  'edit-property': [property: any]
  'delete-property': [property: any]
}>()

// 响应式数据
const dialogVisible = ref(false)
const propertyList = ref<any[]>([])

// 监听visible变化
watch(
  () => props.visible,
  (newVal) => {
    dialogVisible.value = newVal
  },
  { immediate: true }
)

// 监听propertyList变化
watch(
  () => props.propertyList,
  (newVal) => {
    propertyList.value = newVal || []
  },
  { immediate: true, deep: true }
)

// 获取数据类型标签样式
const getDataTypeTag = (type: string) => {
  switch (type) {
    case 'int': return 'primary'
    case 'float': return 'success'
    case 'double': return 'success'
    case 'text': return 'warning'
    case 'date': return 'info'
    case 'bool': return 'danger'
    case 'enum': return 'primary'
    case 'struct': return 'warning'
    case 'array': return 'info'
    default: return 'primary'
  }
}

// 获取数据类型文本
const getDataTypeText = (type: string) => {
  switch (type) {
    case 'int': return '整数型'
    case 'float': return '单精度浮点型'
    case 'double': return '双精度浮点型'
    case 'text': return '字符串'
    case 'date': return '时间型'
    case 'bool': return '布尔型'
    case 'enum': return '枚举型'
    case 'struct': return '结构体'
    case 'array': return '数组'
    default: return '未知'
  }
}

// 获取读写类型标签样式
const getAccessModeTag = (mode: string) => {
  switch (mode) {
    case 'r': return 'info'
    case 'w': return 'warning'
    case 'rw': return 'success'
    default: return 'primary'
  }
}

// 获取读写类型文本
const getAccessModeText = (mode: string) => {
  switch (mode) {
    case 'r': return '只读'
    case 'w': return '只写'
    case 'rw': return '读写'
    default: return '未知'
  }
}

// 新增属性
const addProperty = () => {
  emit('add-property')
}

// 导入属性
const importProperty = () => {
  emit('import-property')
}

// 编辑属性
const editProperty = (property: any) => {
  emit('edit-property', property)
}

// 删除属性
const deleteProperty = (property: any) => {
  ElMessageBox.confirm('确认删除属性"' + property.name + '"？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    emit('delete-property', property)
  })
}

// 关闭对话框
const handleClose = () => {
  emit('update:visible', false)
}
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
</style>